<?php 
/* Real Simple Login v0.1c
 * By Dwarf2000
 * Made for dwarf2000.com and leetcoders.org
 * Released under GNU GPL v3 (http://www.gnu.org/licenses/gpl.html)
*/

$config = array(
"action" => "message", 
"redirecturl" => "download/file.zip", 
"loggedin" => false, 
"usernames" =>  array("user", "user2", "dwarf2000"), 
"passwords" => array("password", "password2", "mypass"), 
"output" => "", 
"message" => "Welcome, user", 

);

$action = "message"; // message or redirect
$redirecturl = "downloads/file.zip";

if (isset($_POST['submit']))
{
	$formuser = $_POST['username'];
	$formpass = $_POST['password'];
	if (in_array($formuser, $config['usernames']))
	{
		$key = array_search($formuser, $config['usernames']);
		if ($formpass == $config['passwords'][$key])
		{
			$config['output'] = "Login successful!<br>";
			$config['loggedin'] = true;
			// TODO: set cookie
			// TODO: make secure cookie system (md5, sha1?)
			switch($config['action'])
			{
				case "redirect":
					header("Location: $redirecturl");
					break;
				case "message":
					$config['message'] = "Welcome, $formuser";
					break;
				default:
					$config['message'] = "Welcome, $formuser";
					break;
			}
		}
		else
		{
			$config['output'] = "Incorrect username/password combination.<br>";
			$config['loggedin'] = false;
		}
	}
	else
	{
		$config['output'] = "Username was not found.<br>";
	}
	$config['output'] = $config['output']."Form data: <span style=\"color:#0066FF\">".$formuser."</span> <span style=\"color:#FF0000\">|</span> <span style=\"color:#0066FF\">".$formpass."</span>";
	$config['output'] = "<p>".$config['output']."</p>\n";
}
// TODO: check if cookie is set
?>
<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="content-type" content="text/html; charset=utf-8">
		<title>Real Simple Login</title>
	</head>
	<body>
		<h1>Real Simple Login</h1>
		<?php echo $config['output']; ?>
		<p>
			<?php if (!$config['loggedin']) { ?><form name="loginform" action="index.php" method="post">
				<label for="username">Username:</label><input type="text" name="username" required>
				<label for="password">Password:</label><input type="password" name="password" required>
				<input type="submit" name="submit" required>
			</form><?php } else { echo $config['message']; } ?>
		</p>
	</body>
</html>